package net.mujiwulian.controller;

import lombok.extern.slf4j.Slf4j;
import net.mujiwulian.entity.Company;
import net.mujiwulian.service.CompanyService;
import net.mujiwulian.vo.Result;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author: silence
 * @Description:
 * @Date:Create：in 2019/12/30 18:54
 */
@Slf4j
@RestController
@RequestMapping("/company")
public class CompanyController {
    @Value("${file-path}")
    private String filePath;
    private final CompanyService companyService;

    public CompanyController(CompanyService companyService) {
        this.companyService = companyService;
    }

    @GetMapping("/select")
    public Result<Company> select() {
        return new Result<>(Result.CodeStatus.OK, "获取公司信息", companyService.getById(1));
    }

    @PostMapping("/update")
    public Result<Void> update(Company company) {
        company.setId(1);
        Company c = companyService.getById(1);
        try {
            if (company.getAptitude1() != null) {
                String path = "/files/company/" + UUID.randomUUID().toString() + company.getAptitude1().getOriginalFilename();
                FileUtils.copyInputStreamToFile(company.getAptitude1().getInputStream(), new File(this.filePath + path));
                company.setAptitude1Url(path);
                log.warn("删除文件:" + c.getAptitude1Url() + "-----" + new File(this.filePath + c.getAptitude1Url()).delete());
            }
            if (company.getAptitude2() != null) {
                String path = "/files/company/" + UUID.randomUUID().toString() + company.getAptitude2().getOriginalFilename();
                FileUtils.copyInputStreamToFile(company.getAptitude2().getInputStream(), new File(this.filePath + path));
                company.setAptitude2Url(path);
                log.warn("删除文件:" + c.getAptitude2Url() + "-----" + new File(this.filePath + c.getAptitude2Url()).delete());
            }
            if (company.getAptitude3() != null) {
                String path = "/files/company/" + UUID.randomUUID().toString() + company.getAptitude3().getOriginalFilename();
                FileUtils.copyInputStreamToFile(company.getAptitude3().getInputStream(), new File(this.filePath + path));
                company.setAptitude3Url(path);
                log.warn("删除文件:" + c.getAptitude3Url() + "-----" + new File(this.filePath + c.getAptitude3Url()).delete());
            }
            if (company.getAptitude4() != null) {
                String path = "/files/company/" + UUID.randomUUID().toString() + company.getAptitude4().getOriginalFilename();
                FileUtils.copyInputStreamToFile(company.getAptitude4().getInputStream(), new File(this.filePath + path));
                company.setAptitude4Url(path);
                log.warn("删除文件:" + c.getAptitude4Url() + "-----" + new File(this.filePath + c.getAptitude4Url()).delete());
            }
            if (company.getLocation() != null) {
                String path = "/files/company/" + UUID.randomUUID().toString() + company.getLocation().getOriginalFilename();
                FileUtils.copyInputStreamToFile(company.getLocation().getInputStream(), new File(this.filePath + path));
                company.setLocationUrl(path);
                log.warn("删除文件:" + c.getLocationUrl() + "-----" + new File(this.filePath + c.getLocationUrl()).delete());
            }
            if (company.getContactImg() != null) {
                String path = "/files/company/" + UUID.randomUUID().toString() + company.getContactImg().getOriginalFilename();
                FileUtils.copyInputStreamToFile(company.getContactImg().getInputStream(), new File(this.filePath + path));
                company.setContactImgUrl(path);
                log.warn("删除文件:" + c.getContactImgUrl() + "-----" + new File(this.filePath + c.getContactImgUrl()).delete());
            }
            if (company.getContactAccount() != null) {
                String path = "/files/company/" + UUID.randomUUID().toString() + company.getContactAccount().getOriginalFilename();
                FileUtils.copyInputStreamToFile(company.getContactAccount().getInputStream(), new File(this.filePath + path));
                company.setContactAccountUrl(path);
                log.warn("删除文件:" + c.getContactAccountUrl() + "-----" + new File(this.filePath + c.getContactAccountUrl()).delete());
            }
            if (company.getContactCompany() != null) {
                String path = "/files/company/" + UUID.randomUUID().toString() + company.getContactCompany().getOriginalFilename();
                FileUtils.copyInputStreamToFile(company.getContactCompany().getInputStream(), new File(this.filePath + path));
                company.setContactCompanyUrl(path);
                log.warn("删除文件:" + c.getContactCompanyUrl() + "-----" + new File(this.filePath + c.getContactCompanyUrl()).delete());
            }
            if (company.getContactLog() != null) {
                String path = "/files/company/" + UUID.randomUUID().toString() + company.getContactLog().getOriginalFilename();
                FileUtils.copyInputStreamToFile(company.getContactLog().getInputStream(), new File(this.filePath + path));
                company.setContactLogUrl(path);
                log.warn("删除文件:" + c.getContactLogUrl() + "-----" + new File(this.filePath + c.getContactLogUrl()).delete());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result<>("操作", companyService.updateById(company));
    }
}
